package org.example.system.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.beans.BeanCopier;

import java.util.ArrayList;
import java.util.List;

/**
 * 对象复制工具类
 * @author joey
 */
public class JoeBeanCopyUtil{

    private static final Logger logger = LoggerFactory.getLogger(JoeBeanCopyUtil.class);

    public static JoeBeanCopyUtil create(){
        return new JoeBeanCopyUtil();
    }
    /**
     * 列表复制
     *
     * @param list
     * @param source 新
     * @return
     */
    public <V,T> List<V> copyList(List<T> list, Class<V> source){
        if(list==null||list.size()==0){
            return new ArrayList<V>();
        }
        try {
            List<V> result = new ArrayList<>(list.size());
            BeanCopier beanCopier = BeanCopier.create(list.get(0).getClass(),source,  false);
            for(T t : list){
                V v = (V)source.newInstance();
                beanCopier.copy(t,v,null);
                result.add(v);
            }
            return result;
        }catch (Exception e){
            logger.error("列表复制出错",e);
            throw new RuntimeException("列表复制出错");
        }
    }

}
